package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.SysOperLog;
import com.example.mapper.SysOperLogMapper;
import com.example.service.SysOperLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 操作日志服务实现类
 */
@Service
@RequiredArgsConstructor
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Override
    public Page<SysOperLog> selectOperLogPage(Page<SysOperLog> page, SysOperLog operLog) {
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        
        // 构建查询条件
        if (operLog.getTitle() != null && !operLog.getTitle().isEmpty()) {
            wrapper.like(SysOperLog::getTitle, operLog.getTitle());
        }
        if (operLog.getBusinessType() != null) {
            wrapper.eq(SysOperLog::getBusinessType, operLog.getBusinessType());
        }
        if (operLog.getStatus() != null) {
            wrapper.eq(SysOperLog::getStatus, operLog.getStatus());
        }
        if (operLog.getOperName() != null && !operLog.getOperName().isEmpty()) {
            wrapper.like(SysOperLog::getOperName, operLog.getOperName());
        }
        
        // 按操作时间降序排序
        wrapper.orderByDesc(SysOperLog::getOperTime);
        
        return page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOperLog(SysOperLog operLog) {
        save(operLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOperLogByIds(List<Long> operIds) {
        return baseMapper.deleteBatchIds(operIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cleanOperLog() {
        baseMapper.delete(new LambdaQueryWrapper<>());
    }
} 